package base.dao.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import base.dao.BaseDao;
import common.db.DBUtil;
import tmall.company.entity.CompanyEntity;

/*Connection conn = DBUtil.getConn();
String sql="INSERT INTO tmall_company_t(NAME,ADDRESS,TELEPHONE,BOSS) VALUES (?,?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,entity.getName());
stmt.setString(2,entity.getAddress());
stmt.setString(3,entity.getTelephone());
stmt.setString(4,entity.getBoss());
int count = stmt.executeUpdate();
return count;*/

public class BaseDaoImpl<T> implements BaseDao<T> {
	
	private String sql;
	
	//注入sql
	public void setSql(String sql) {
		this.sql = sql;
	}

	@Override
	public int add(T entity) throws Exception {
		Connection conn = DBUtil.getConn();
		PreparedStatement stmt = conn.prepareStatement(sql);
		//stmt.setString(1,entity.getName());
//		INSERT INTO tmall_company_t(NAME,ADDRESS,TELEPHONE,BOSS) VALUES (?,?,?,?)
		//反射
		/**
		 * 1.数据库的字段一样,NAME,ADDRESS,TELEPHONE,BOSS
		 */
		
		String newsql=remove_(sql);
		//截断字段
		String substring = newsql.substring(newsql.indexOf("(")+1,newsql.indexOf(")"));
		String[] splits = substring.split(",");
		Field[] fields = entity.getClass().getDeclaredFields();
		Method[] methods = entity.getClass().getDeclaredMethods();
		int i=1;
		for (String str : splits) {
			
			//表的字段与截取的字段比较
			for (Field field : fields) {
				
				int count=0;
				String fieldName=field.getName();
				if(str.equalsIgnoreCase(fieldName)){
					//stmt.setString(1,entity.getName());
					for (Method method : methods) {
						String methodName = method.getName();
						
						//找到具体的get方法
						if(methodName.equalsIgnoreCase("get"+fieldName)){
							stmt.setObject(i, method.invoke(entity));
							i++;
							count++;
							break;
						}
					}
				}
				if(count==1){
					break;
				}
			}
		}
		int count = stmt.executeUpdate();
		return count;
	}

	/*Connection conn = DBUtil.getConn();
	String sql="UPDATE tmall_company_t SET is_delete=0 WHERE id=?";
	PreparedStatement stmt = conn.prepareStatement(sql);
	stmt.setInt(1,id);
	int count = stmt.executeUpdate();
	return count;
	*/
	@Override
	public int deleteById(Integer id) throws Exception {
		Connection conn = DBUtil.getConn();
		PreparedStatement stmt = conn.prepareStatement(sql);
		String newsql = remove_(sql);
		stmt.setInt(1,id);
		int count = stmt.executeUpdate();
		return count;
	}


	
	@Override
	public int deleteById(String id) throws Exception {

		
		return 0;
	}
	
	/*	Connection conn = DBUtil.getConn();
	String sql="update tmall_company_t SET NAME=?,ADDRESS=?,TELEPHONE=?,BOSS=? WHERE id=?";
	PreparedStatement stmt = conn.prepareStatement(sql);
	
	stmt.setString(1,entity.getName());
	stmt.setString(2,entity.getAddress());
	stmt.setString(3,entity.getTelephone());
	stmt.setString(4,entity.getBoss());
	
	stmt.setInt(5,entity.getId());
	int count = stmt.executeUpdate();
	return count;*/
	
	@Override
	public int update(T t) throws Exception {
		
		
		
		return 0;
	}

	@Override
	public T findById(Integer id) throws Exception {
		
		
		Connection conn = DBUtil.getConn();
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		
		while(rs.next()){
			
		}
		
		return null;
	}
	
	/*Connection conn = DBUtil.getConn();
	String sql="SELECT ID AS id,NAME AS name,ADDRESS AS address,TELEPHONE AS telephone,BOSS AS boss from tmall_company_t WHERE ID=? AND IS_DELETE=1";
	PreparedStatement stmt = conn.prepareStatement(sql);
	stmt.setString(1, id);
	ResultSet rs = stmt.executeQuery();
	CompanyEntity entity=new CompanyEntity();
	while(rs.next()){
		entity.setId(rs.getInt("id"));
		entity.setName(rs.getString("name"));
		entity.setAddress(rs.getString("address"));
		entity.setBoss(rs.getString("boss"));
		entity.setTelephone(rs.getString("telephone"));
	}
	return entity;*/
	
	@Override
	public T findById(String id) throws Exception {
	
		return null;
	}
	
	@Override
	public List<T> findAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findWithPage(Integer currentPage, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	//去下划线
	public static String remove_(String str){
		String[] split = str.split("_");
		String newString="";
		for (String s : split) {
			newString+=s;
		}
		return newString;
	}

}
